package com.preguicoso.client.estabelecimento.cardapio.modelosItens;

import java.util.ArrayList;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.RadioButton;
import com.preguicoso.client.estabelecimento.cardapio.CardapioService;
import com.preguicoso.client.estabelecimento.cardapio.CardapioServiceAsync;
import com.preguicoso.client.estabelecimento.carrinho.Carrinho;
import com.preguicoso.client.estabelecimento.carrinho.CarrinhoPedidosUI;
import com.preguicoso.client.telaInicial.lightBox;
import com.preguicoso.shared.entities.ItemCardapioBean;
import com.preguicoso.shared.utils.MoneyUtils;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.event.dom.client.ChangeEvent;

public class PizzaTipo extends Composite {

	private static PizzaTipoUiBinder uiBinder = GWT
			.create(PizzaTipoUiBinder.class);
	
	@UiField Image image;
	@UiField Label nome;
	@UiField Label de;
	@UiField Label por;
	@UiField Label descricao;
	@UiField Button adicionar;
	@UiField Label qtdem;
	
	@UiField RadioButton pequena;
	@UiField RadioButton media;
	@UiField RadioButton grande;
	@UiField RadioButton inteira;
	@UiField RadioButton metade;
	@UiField ListBox opcoes;
	@UiField Label preco;
	
	ItemCardapioBean i;

	interface PizzaTipoUiBinder extends UiBinder<Widget, PizzaTipo> {
	}

	public PizzaTipo() {
		initWidget(uiBinder.createAndBindUi(this));
	}
	
	private final CardapioServiceAsync cardapioService = GWT
			.create(CardapioService.class);

	public PizzaTipo(final ItemCardapioBean i) {
		initWidget(uiBinder.createAndBindUi(this));
		this.i = i;
		opcoes.setVisible(false);
		image.setUrl(i.getImagem());
		nome.setText(i.getNome());
		preco.setText("R$ "+MoneyUtils.parseString(i.getPreco()));por.setText("R$ "+MoneyUtils.parseString(i.getPreco()));
		por.setText("R$ "+MoneyUtils.parseString(i.getPreco()));por.setText("R$ "+MoneyUtils.parseString(i.getPreco()));
		descricao.setText(i.getDescricao());
		opcoes.addItem("Carregando Opções");
		cardapioService.getItensCardapioByType(i.getEstabelecimentoBean(), "sushi", new AsyncCallback<ArrayList<ItemCardapioBean>>() {
			
			@Override
			public void onSuccess(ArrayList<ItemCardapioBean> result) {
				if(result.size()==0)
				for (ItemCardapioBean itemCardapioBean : result) {
					opcoes.addItem(itemCardapioBean.getNome()+ "R$ "+itemCardapioBean.getPreco(), itemCardapioBean.getPreco()+"");
				}
				
			}
			
			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}
		});
	}
	
	String Size = "Grande";
	String Tipo = "";
	
	@UiHandler("adicionar")
	void onAdicionarClick(ClickEvent event) {
		ItemCardapioBean e;
		if(Tipo.equals("Metade")){
			if(Long.parseLong(opcoes.getValue(opcoes.getSelectedIndex()))>i.getPreco())
				i.setPreco(Long.parseLong(opcoes.getValue(opcoes.getSelectedIndex())));
			e = new ItemCardapioBean("Pizza metade "+i.getNome()+"e metade "+opcoes.getItemText(opcoes.getSelectedIndex()).split("R$")[0], i.getTipo(),i.isDisponivel(), i.getDescricao(), Long.parseLong(preco.getText()), i.getCategoriaBean(), i.getEstabelecimentoBean());
		}
		else{
			e=i;
		}
		Carrinho.getInstance().addItem(e);
		Carrinho.getInstance().refreshCarrinho();
	
//		cardapioService.addItem(e, 1, "", new AsyncCallback<Void>() {
//			
//			@Override
//			public void onSuccess(Void result) {
//				lightBox.getInstance().hide();
//				CarrinhoPedidosUI.getInstance().atualizarCarrinhoPedidos();
//			}
//			
//			@Override
//			public void onFailure(Throwable caught) {
//				// TODO Auto-generated method stub
//				
//			}
//		});
		
	}
	@UiHandler("metade")
	void onMetadeClick(ClickEvent event) {
		Tipo = "Metade";
		opcoes.setVisible(true);
	}
	@UiHandler("inteira")
	void onInteiraClick(ClickEvent event) {
		Tipo = "";
		opcoes.setVisible(false);
	}
	@UiHandler("pequena")
	void onPequenaClick(ClickEvent event) {
		Size = "pequena";
		preco.setText("R$ "+0.50*i.getPreco());
	}
	
	@UiHandler("media")
	void onMediaClick(ClickEvent event) {
		Size = "Media";
		preco.setText("R$ "+0.80*i.getPreco());
	}
	@UiHandler("grande")
	void onGrandeClick(ClickEvent event) {
		Size = "Grande";
		preco.setText("R$ "+i.getPreco());
	}
	@UiHandler("opcoes")
	void onOpcoesChange(ChangeEvent event) {
		if(Long.parseLong(opcoes.getValue(opcoes.getSelectedIndex()))>i.getPreco())
			preco.setText(MoneyUtils.parseString(Long.parseLong(opcoes.getValue(opcoes.getSelectedIndex()))));
		else{
			preco.setText(i.getPreco()+"");
		}
		
	}
}
